package com.nlscan.qdkit.ui.publico.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class ImageUtil {

    public static byte[] Bitmap2Bytes(Context context, Uri uri, int size) {
        InputStream input;
        Bitmap bitmap = null;

        try {
            int be = 1;

            input = context.getContentResolver().openInputStream(uri);
            BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
            bitmapOptions.inJustDecodeBounds = true;
            bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions);

            int width = bitmapOptions.outWidth;
            int height = bitmapOptions.outHeight;

            while (width / be > 1920 || height / be > 1080) {
                be++;
            }

            bitmapOptions.inJustDecodeBounds = false;
            bitmapOptions.inSampleSize = be;
            bitmapOptions.inDither = true;
            bitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565;

            input = context.getContentResolver().openInputStream(uri);

            bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions);
            input.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (bitmap == null) {
            return null;
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);

        int scale = 100;

        while (baos.toByteArray().length / 1024 > size) {
            scale -= 10;
            baos.reset();
            bitmap.compress(Bitmap.CompressFormat.JPEG, scale, baos);
        }

        return baos.toByteArray();
    }

    /**
     * 读取图片属性：旋转的角度      * @param path 图片绝对路径      * @return degree旋转的角度
     */
    public static int readPictureDegree(String path) {
        int degree = 0;
        try {
            ExifInterface exifInterface = new ExifInterface(path);
            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return degree;
    }
    /*     * 旋转图片      * @param angle      * @param bitmap      * @return Bitmap      */

    public static Bitmap rotaingImageView(int angle, Bitmap bitmap) {          //旋转图片 动作
        Matrix matrix = new Matrix();
        ;
        matrix.postRotate(angle);
        System.out.println("angle2=" + angle);          // 创建新的图片
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        return resizedBitmap;
    }


    /**
     * 二维码是生成
     *
     * @param url
     * @param width
     * @param height
     * @return
     */
//    public static Bitmap createQRImage(String url, final int width, final int height) {
//        try {
//            // 判断URL合法性
//            if (url == null || "".equals(url) || url.length() < 1) {
//                return null;
//            }
//            Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
//            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
//            // 图像数据转换，使用了矩阵转换
//            BitMatrix bitMatrix = new QRCodeWriter().encode(url,
//                    BarcodeFormat.QR_CODE, width, height, hints);
//            int[] pixels = new int[width * height];
//            // 下面这里按照二维码的算法，逐个生成二维码的图片，
//            // 两个for循环是图片横列扫描的结果
//            for (int y = 0; y < height; y++) {
//                for (int x = 0; x < width; x++) {
//                    if (bitMatrix.get(x, y)) {
//                        pixels[y * width + x] = 0xff000000;
//                    } else {
//                        pixels[y * width + x] = 0xffffffff;
//                    }
//                }
//            }
//            // 生成二维码图片的格式，使用ARGB_8888
//            Bitmap bitmap = Bitmap.createBitmap(width, height,
//                    Bitmap.Config.ARGB_8888);
//            bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
//            return bitmap;
//        } catch (WriterException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }


}
